package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWeightProductionKitchenInfoMapper;
import com.guofen.dao.SjqWeightProductionKitchenMapper;
import com.guofen.pojo.SjqFlocculationProduceCheck;
import com.guofen.pojo.SjqWeightProductionKitchen;
import com.guofen.pojo.SjqWeightProductionKitchenInfo;
import com.guofen.service.sjq.SjqWeightProductionKitchenInfoService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqWeightProductionKitchenInfoServiceImpl implements SjqWeightProductionKitchenInfoService {

    @Autowired
    SjqWeightProductionKitchenInfoMapper sjqWeightProductionKitchenInfoMapper;

    @Autowired
    SjqWeightProductionKitchenMapper sjqWeightProductionKitchenMapper;

    @Override
    public List<Boolean> addFromKitchenExcel(List<SjqWeightProductionKitchenInfo> sjqWeightProductionKitchenInfoList) {
        List<Boolean> registerResult = new ArrayList<>();
        SjqWeightProductionKitchen sjqWeightProductionKitchen = new SjqWeightProductionKitchen();
        int list1[] = {0,0};
        double list2[] = {0.0,0.0};
        for (SjqWeightProductionKitchenInfo s : sjqWeightProductionKitchenInfoList){
            System.out.println();
            int temp = sjqWeightProductionKitchenInfoMapper.insert(s);
            registerResult.add(temp == 1);
            if (s.getCarryCompany().equals("海淀环卫五队")) {list1[0]++; list2[0]+=s.getNetWeight();}
            if (s.getCarryCompany().equals("海淀环卫八队")) {list1[1]++; list2[1]+=s.getNetWeight();}
        };
        sjqWeightProductionKitchen.setTrain1(list1[0]);
        sjqWeightProductionKitchen.setTrain2(list1[1]);
        sjqWeightProductionKitchen.setWeight1(list2[0]);
        sjqWeightProductionKitchen.setWeight2(list2[1]);
        String date = DateAndTimeUtil.getDate();
        sjqWeightProductionKitchen.setDate(date);
        sjqWeightProductionKitchen.setTrainTotal(list1[0]+list1[1]);
        sjqWeightProductionKitchen.setWeightTotal(list2[0]+list2[1]);
        registerResult.add(sjqWeightProductionKitchenMapper.insert(sjqWeightProductionKitchen) == 1);
        return registerResult;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWeightProductionKitchenInfo> list = sjqWeightProductionKitchenInfoMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWeightProductionKitchenInfo> list = sjqWeightProductionKitchenInfoMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWeightProductionKitchenInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWeightProductionKitchenInfo record) {
        return sjqWeightProductionKitchenInfoMapper.updateByPrimaryKeySelective(record);
    }
}
